package stack_queue;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description: 225. 用队列实现栈
 * User: Li_yizYa
 * Date: 2025/5/17
 * Time: 23:24
 */
public class Solution2 {
    Queue<Integer> queue1;
    Queue<Integer> queue2; // 辅助队列

    public Solution2() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        queue2.offer(x);
        while (!queue1.isEmpty()) {
            queue2.offer(queue1.poll());
        }
        Queue<Integer> tmpQueue = queue1;
        queue1 = queue2;
        queue2 = tmpQueue;
    }

    public int top() {
        if (queue1.isEmpty()) {
            return -1;
        }
        return queue1.peek();
    }

    public int pop() {
        if (queue1.isEmpty()) {
            return -1;
        }
        return queue1.poll();
    }

    public boolean empty() {
        return queue1.isEmpty();
    }

    public static void main(String[] args) {

    }
}
